package com.amado.course17bothwayNode;

import java.util.Scanner;

import com.amado.course16SinglyNode.SinglyNode;

/**
 * 数据结构-单向链表 数组通过下标访问 链表只能按循序一个一个查找
 * 
 * @author wangyulong
 * @date 2018-06-21
 *
 */
public class SinglyLinkedListTemp {
	Scanner scanner = new Scanner(System.in);
	Node fristNode = null;
	Node tailNode = null;

	SinglyLinkedListTemp() {
		init();
	}

	void init() {
		fristNode = new Node("node1", "节点1");
		Node node2 = new Node("node2", "节点2");
		Node node3 = new Node("node3", "节点3");
		fristNode.setNext(node2);
		node2.setNext(node3);
		node3.setPrevious(node2);
		node2.setPrevious(fristNode);
		tailNode = node3;
	}

	/**
	 * 根据nodeCode获取节点对象
	 * 
	 * @param nodeCode
	 * @return 不存在返回null
	 */
	public Node getNode(String nodeCode) {
		Node nextNode = fristNode;
		// 判断下一个节点不能为空
		while (nextNode != null) {
			if (nextNode.getCode().equals(nodeCode)) {
				return nextNode;
			}
			nextNode = nextNode.getNext();
		}
		return null;
	}

	/**
	 * 打印节点
	 */
	public void printNodeByHead() {
		Node node = fristNode;
		while (node != null) {
			System.out.print(node.getName() + "\t");
			node = node.getNext();
		}
	}

	/**
	 * 打印节点
	 */
	public void printNodeByTail() {
		Node node = tailNode;
		while (node != null) {
			System.out.print(node.getName() + "\t");
			node = node.getPrevious();
		}
	}

	public static void main(String[] args) {
		SinglyLinkedListTemp singlyLinkedList = new SinglyLinkedListTemp();
		while (true) {
			System.out.println("\n1、添加2、删除  3、查询");
			int menuSelect = singlyLinkedList.scanner.nextInt();
			if (menuSelect == 1) {
				singlyLinkedList.add();
			} else if (menuSelect == 2) {
				singlyLinkedList.delete();
			} else if (menuSelect == 3) {
				singlyLinkedList.printNodeByHead();
				System.out.println();
				singlyLinkedList.printNodeByTail();
			}
		}
	}

	public void add() {
		if (fristNode != null) {
			System.out.println("请输入你要插入的节点后位置");
			String afterNodeCode = scanner.next();
			Node afterNode = getNode(afterNodeCode);
			// 不存在
			if (afterNode == null) {
				System.out.println("你输入的节点不存在");
				return;
			}
			System.out.println("请输入你的节点code");
			String nodeCode = scanner.next();
			// 已存在
			if (getNode(nodeCode) != null) {
				System.out.println("请输入你的节点CODE已经存在");
				return;
			}
			System.out.println("请输入你的节点name");
			String nodeName = scanner.next();
			// 新节点
			Node newNode = new Node(nodeCode, nodeName);
			// afterNode CODE2
			// nextNode CODE3
			Node nextNode = afterNode.getNext();

			afterNode.setNext(newNode);
			newNode.setNext(nextNode);

			newNode.setPrevious(afterNode);
			nextNode.setPrevious(newNode);
		} else {
			System.out.println("请输入你的节点code");
			String nodeCode = scanner.next();
			System.out.println("请输入你的节点name");
			String nodeName = scanner.next();
			Node newNode = new Node(nodeCode, nodeName);
			fristNode = newNode;
			tailNode = newNode;
		}
	}

	public void delete() {
		System.out.println("请输入你要删除的节点后位置");
		String deleteNodeCode = scanner.next();
		delete(deleteNodeCode);

	}

	public void delete(String nodeCode) {
		Node node = fristNode;
		while (node != null) {
			// Node2
			if (node.getCode().equals(nodeCode)) {
				if (node.equals(fristNode)) {
					fristNode = node.getNext();
					// 不是最后一个节点了
					if (fristNode != null) {
						fristNode.setPrevious(null);
					} else {
						tailNode = null;
					}
				} else if (node.equals(tailNode)) {
					tailNode = node.getPrevious();
					tailNode.setNext(null);
				} else {
					// NODE3
					Node nextNode = node.getNext();
					// NODE1
					Node previous = node.getPrevious();
					previous.setNext(nextNode);
					nextNode.setPrevious(previous);
				}
			}
			node = node.getNext();
		}
	}
}
